﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Common;
using SDLITS.Model;
using MySoft.Data;
namespace SDLITS.Web.SystemManage
{
    public partial class CompanyUserChange : CommonBase
    {
        #region 页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCom();
                LoadData();
            }
        }
        #endregion
        protected void BindCom()
        {
            List<Company> lists = SDDbSession.Default.From<Company>().Where(Company._.StatusID != 3).ToList();
            drp_com.DataSource = lists;
            drp_com.DataValueField = "CompanyID";
            drp_com.DataTextField = "CompanyName";
            drp_com.DataBind();

        }
        #region 读取数据


        private void LoadData()
        {
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                User Tm = SDDbSession.Default.From<User>().Where(SDLITS.Model.User._.UserID == id).ToFirst();
                txt_brith.SelectedDate = Tm.Birthday;
                txt_email.Text = Tm.SMTPFromEmail;
                txt_phone.Text = Tm.Phone1;
                txt_PWD.Text = Tm.UserPwd;
                txt_RealName.Text = Tm.RealName;
                txt_tel.Text = Tm.Phone2;
                txt_UserName.Text = Tm.UserName;
                drp_com.SelectedValue = Tm.CompanyID.ToString();
                rbl_login.SelectedValue = Tm.StatusID.ToString();
                rbl_sex.SelectedValue = Convert.ToBoolean(Tm.Sex) ? "1" : "2";
            }
        }
        #endregion

        #region 保存数据
        
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            User Tm = new User();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                if (SDDbSession.Default.From<User>().Where(SDLITS.Model.User._.UserName == txt_UserName.Text.Trim() && SDLITS.Model.User._.UserID!=Request["id"]).Count() > 0)
                {
                    Alert.Show("用户名" + txt_UserName.Text.Trim() + "已存在，请修改后再添加");
                    return;
                }
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                Tm.UserID = int.Parse(Request["id"]);
                Tm.Attach();
                Tm.Birthday = string.IsNullOrEmpty(txt_brith.Text) ? DateTime.Parse("1900-1-1 00:00:00") : DateTime.Parse(txt_brith.Text);
                Tm.SMTPFromEmail = txt_email.Text;
                Tm.Phone1 = txt_phone.Text;
                Tm.UserPwd = string.IsNullOrEmpty(txt_PWD.Text) ? "888888" : txt_PWD.Text.Trim();
                Tm.RealName = txt_RealName.Text;
                Tm.Phone2 = txt_tel.Text;
                Tm.UserName = txt_UserName.Text.Trim();
                Tm.StatusID = int.Parse(rbl_login.SelectedValue);
                Tm.Sex = rbl_sex.SelectedValue == "1" ? true : false;
                Tm.ModifyDate = DateTime.Now;
                Tm.ModifyUserID = user.UserID;
                Tm.CompanyID = int.Parse(drp_com.SelectedValue);  
                returnvalue = SDDbSession.Default.Update<User>(Tm);
            }
            else
            {
                if (SDDbSession.Default.From<User>().Where(SDLITS.Model.User._.UserName == txt_UserName.Text.Trim()).Count() > 0)
                {
                    Alert.Show("用户名" + txt_UserName.Text.Trim() + "已存在，请修改后再添加");
                    return;
                }
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                Tm.DeAttach();
                Tm.Birthday = string.IsNullOrEmpty(txt_brith.Text) ? DateTime.Parse("1900-1-1 00:00:00") : DateTime.Parse(txt_brith.Text);
                Tm.SMTPFromEmail = txt_email.Text;
                Tm.Phone1 = txt_phone.Text;
                Tm.UserPwd =string.IsNullOrEmpty (txt_PWD.Text)?"888888": txt_PWD.Text.Trim();
                Tm.RealName = txt_RealName.Text;
                Tm.Phone2 = txt_tel.Text;
                Tm.UserName = txt_UserName.Text;
                Tm.StatusID = int.Parse(rbl_login.SelectedValue);
                Tm.Sex = rbl_sex.SelectedValue == "1" ? true : false;
                Tm.ModifyDate = DateTime.Now;
                Tm.ModifyUserID = user.UserID;
                Tm.CreateDate = DateTime.Now;
                Tm.CreateUserID = user.UserID;
                Tm.LoginTime = DateTime.Now;
                Tm.MemCardID = -1;
                Tm.PositionID = -1;
                Tm.CompanyID = int.Parse(drp_com.SelectedValue);               
                returnvalue = SDDbSession.Default.Insert<User>(Tm);
                UserInRole ur = new UserInRole();
                ur.DeAttach();
                ur.RoleID = 23;
                ur.UserID = returnvalue;
                SDDbSession.Default.Insert<UserInRole>(ur);
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        #endregion
    }
}